<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="查询时间" prop="propertyName">
        <el-date-picker v-model="dateRange" clearable size="mini" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="正常" name=1 />
      <el-tab-pane label="迟到" name=2 />
      <el-tab-pane label="旷工" name=3 />
      <el-tab-pane label="请假" name=4 />
    </el-tabs>
    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <router-link :to="'/property/office/attendance/group?propertyId='+ property" class="link-type">
          <el-button icon="el-icon-back" size="mini">
            返 回
          </el-button>
        </router-link>
      </el-col>

      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['module:attendanceList:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
          v-hasPermi="['module:attendanceList:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attendanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物业公司" align="center" prop="propertyName" />
      <el-table-column label="所属小区" align="center" prop="estateName" />
      <el-table-column label="员工编号" align="center" prop="sysUserId" />
      <el-table-column label="员工姓名" align="center" prop="sysUserName"/>
      <el-table-column label="打卡状态" align="center" prop="state" width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 1" style="cursor: pointer" :type="''">正常</el-tag>
          <el-tag v-if="scope.row.state === 2" style="cursor: pointer" :type="''">迟到</el-tag>
          <el-tag v-if="scope.row.state === 3" style="cursor: pointer" :type="''">旷工</el-tag>
          <el-tag v-if="scope.row.state === 4" style="cursor: pointer" :type="''">请假</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="请假状态" align="center" prop="holidayState" width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.holidayState === null" style="cursor: pointer" :type="''">未请假</el-tag>
          <el-tag v-if="scope.row.holidayState === 0" style="cursor: pointer" :type="''">未处理</el-tag>
          <el-tag v-if="scope.row.holidayState === 1" style="cursor: pointer" :type="''">未批准</el-tag>
          <el-tag v-if="scope.row.holidayState === 2" style="cursor: pointer" :type="''">已批准</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="请假的审核时间" align="center" prop="holidayCheckTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.holidayCheckTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="审批人" align="center" prop="holidayCheckSysUserId" />

      <el-table-column label="规定打卡时间" align="center" prop="dutyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dutyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="打卡时间" align="center" prop="amOrPm" width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.amOrPm === 0" style="cursor: pointer" :type="''">上午上班</el-tag>
          <el-tag v-if="scope.row.amOrPm === 1" style="cursor: pointer" :type="''">上午下班</el-tag>
          <el-tag v-if="scope.row.amOrPm === 2" style="cursor: pointer" :type="''">下午上班</el-tag>
          <el-tag v-if="scope.row.amOrPm === 3" style="cursor: pointer" :type="''">下午下班</el-tag>
        </template>
      </el-table-column>


<!--      <el-table-column label="账号等级" align="center" prop="userGridLevel" width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.userGridLevel === 1" style="cursor: pointer" :type="''">县级账号</el-tag>
          <el-tag v-if="scope.row.userGridLevel === 2" style="cursor: pointer" :type="''">镇级账号</el-tag>
        </template>
      </el-table-column>-->

      <el-table-column label="打卡状态" align="center" prop="isOut" width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isOut === 0" style="cursor: pointer" :type="''">正常</el-tag>
          <el-tag v-if="scope.row.isOut === 1" style="cursor: pointer" :type="''">外勤打卡</el-tag>
          <el-tag v-if="scope.row.isOut === 2" style="cursor: pointer" :type="''">请假</el-tag>
        </template>
      </el-table-column>

<!--

      <el-table-column label="请假开始日期" align="center" prop="leaveBeginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveBeginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请假结束日期" align="center" prop="leaveEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
-->

      <el-table-column label="考勤组" align="center" prop="dutyGroupName" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['module:attendanceList:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { listAttendance, getAttendance, delAttendance, addAttendance, updateAttendance, exportAttendance } from "@/api/module/attendance/attendance";

export default {
  name: "Attendance",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      activeName:'1',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤表格数据
      attendanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRange:undefined,
      property:undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        propertyId: undefined,
        estateId: undefined,
        deptId: undefined,
        state: 1,
        holidayState: undefined,
        holidayResult: undefined,
        holidayCheckTime: undefined,
        holidayCheckSysUserId: undefined,
        dutyTime: undefined,
        amOrPm: undefined,
        lng: undefined,
        lat: undefined,
        address: undefined,
        gridId: undefined,
        lastGridId: undefined,
        userGridLevel: undefined,
        isOut: undefined,
        imgs: undefined,
        leaveBeginDate: undefined,
        leaveBeginTime: undefined,
        leaveEndDate: undefined,
        leaveEndTime: undefined,
        totalSignCount: undefined,
        dutyGroupId: undefined,
        beginDate: undefined,
        endDate: undefined,
      },
      // 表单参数
      form:{},
      // 表单校验
      rules: {}

    }
  },
  created() {
    this.queryParams.dutyGroupId = this.$route.query.dutyGroupId ? parseInt(this.$route.query.dutyGroupId) : undefined;
    this.property = this.$route.query.propertyId ? this.$route.query.propertyId : undefined;
    this.getList();
  },
  methods: {
    /** 查询考勤列表 */
    getList() {
      this.loading = true;
      listAttendance(this.queryParams).then(response => {
        this.attendanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        propertyId: undefined,
        estateId: undefined,
        sysUserId: undefined,
        deptId: undefined,
        state: undefined,
        holidayState: undefined,
        holidayResult: undefined,
        holidayCheckTime: undefined,
        holidayCheckSysUserId: undefined,
        createTime: undefined,
        dutyTime: undefined,
        amOrPm: undefined,
        lng: undefined,
        lat: undefined,
        address: undefined,
        remark: undefined,
        gridId: undefined,
        lastGridId: undefined,
        userGridLevel: undefined,
        isOut: undefined,
        imgs: undefined,
        leaveBeginDate: undefined,
        leaveBeginTime: undefined,
        leaveEndDate: undefined,
        leaveEndTime: undefined,
        totalSignCount: undefined,
        dutyGroupId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.dateRange !=undefined && this.dateRange.length === 2){
        this.queryParams.beginDate = this.dateRange[0];
        this.queryParams.endDate = this.dateRange[1];
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考勤";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getAttendance(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改考勤";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateGroup(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addGroup(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除现金奖励规则编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delGroup(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出本组所有考勤数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportAttendance(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    handleClick(val){
      console.log('handleClick',val.name);
      this.activeName = val.name;
      this.queryParams.state = parseInt(val.name);
      this.getList();
    },
  }
};
</script>
